<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入菜品名称" suffix-icon="el-icon-search" v-model="dishName"></el-input>
      <el-select v-model="category" placeholder="请选择菜品类型">
        <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>

      <el-button class="ml-5" type="primary" @click="selectById">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>

      <el-button type="primary" @click="exp" class="ml-5">导出 <i class="el-icon-top"></i></el-button>
    </div>
    <el-table
        :data="dishList"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
    >
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column align="center" label="菜品名称" width="95">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>

      <el-table-column label="价格" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column label="销量" width="110" align="center">
        1
      </el-table-column>
      <el-table-column align="center" label="菜品图片" width="95">
        <template slot-scope="scope">
          <el-image style="width: auto; height: 40px; border:none;cursor: pointer;"
                    :src="getImage(scope.row.img)"
                    :preview-src-list="[ `http://localhost:9090/hbgc/download?name=${scope.row.img}` ]">
          </el-image>
        </template>
      </el-table-column>

      <el-table-column label="菜品介绍" align="center">
        <template slot-scope="scope">
          {{ scope.row.introduce }}
        </template>
      </el-table-column>


      <el-table-column label="菜品类型" width="110" align="">
        <template slot-scope="scope">
          <span>{{ scope.row.type }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否下架" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status"
                     :active-value="1"
                     :inactive-value="0"
                     @change=switchStatusMe($event,scope.row.id,scope.row.status)>
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="100"
          align="center">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
          <el-button type="text" size="small" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" append-to-body>
      <el-form
          ref="form"
          :model="form"
          :rules="rules"
          label-width="80px">
        <el-form-item label="菜品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入菜品名称"/>
        </el-form-item>
        <el-form-item label="菜品类型" prop="type">
          <el-select v-model="form.type" placeholder="请输入菜品类型">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="价格(元)" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格"/>
        </el-form-item>

        <el-form-item label="菜品介绍" prop="introdce">
          <el-input v-model="form.introduce" placeholder="请输入菜品介绍" type="textarea"/>
        </el-form-item>
        <el-form ref="form" :model="form" v-model="form.img" :rules="rules" label-width="80px">
          <el-form-item label="图片">

            <el-upload class="upload-demo"
                       ref="upload"
                       action="http://localhost:9090/hbgc/upload"
                       :before-remove="beforeRemove"
                       name="file"
                       :limit="1"
                       :on-exceed="handleExceed"
                       :on-success="handleAvatarSuccess"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传一张jpg/png文件</div>
            </el-upload>
          </el-form-item>
        </el-form>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "MyDish",
  data(){
    return{
      options: [{
        value: '米饭',
        label: '米饭'
      }, {
        value: '面食',
        label: '面食'
      }, {
        value: '炒菜',
        label: '炒菜'
      }, {
        value: '米线',
        label: '米线'
      }, {
        value: '汉堡',
        label: '汉堡'
      }, {
        value: '麻辣烫',
        label: '麻辣烫'
      }, {
        value: '饮品',
        label: '饮品'
      }, {
        value: '其他',
        label: '其他'
      }],
      dishName:'',
      category:'',
      dishList: [],
      form: {},
      rules: {
        phone: [
          {required: true, message: '请输入手机号码', trigger: 'blur'},
          {min: 11, max: 11, message: '请输入11位手机号码', trigger: 'blur'},
          {
            pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
            message: '请输入正确的手机号码'
          }
        ],
      },
      shopId:0,
      title: '',
      dialogFormVisible: false,
    }
  },
  created() {
    this.selectById()
  },
  methods:{

    switchStatusMe($event,id,status){
      console.log(status)
      this.request.put("/updateDishStatus/"+id+'/'+status).then(res=>{
        console.log(res)
      })
    },
    cancel() {
      this.dialogFormVisible = false
    },
    submitForm() {
        this.request.put("/updateDish", this.form).then(res => {
          if (res) {
            this.$message.success("修改成功")
            this.dialogFormVisible = false
            this.selectListPage()
          } else {
            this.$message.error("修改失败")
          }
        })
    },
    handleAvatarSuccess(res, file) {
      this.form.img = res.data;
    },
    beforeRemove(file) {

      return this.$confirm(`确定移除 ${file.name}？`).then(() => {
        console.log(this.form.img)
        this.request.delete("/deleteImg/" + this.form.img)
      })
    },
    handleExceed() {
      this.$message.warning(`当前限制选择 1 个文件`);
    },
    selectById(){
   this.shopId= JSON.parse(localStorage.getItem("user")).shopId
      const data = {
        shopId:this.shopId,
        dishName:this.dishName,
        category:this.category,
      }
      console.log(this.shopId)
      this.request.post("/mySelectMyDish/",data).then(res=>{
        this.dishList=res.data

      })
    },
    del(id) {
      console.log('@@@@'+id)
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/dishDelete/" + id).then(res => {
          if (res.code == 200) {
            this.$message.success("删除成功")
            this.selectById()
          } else {
            this.$message.error("删除失败")
          }
        })
      }).catch(() => {
        //用户点击取消按钮
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    handleEdit(row) {
      this.title = "修改"
      this.form = row
      this.dialogFormVisible = true
      this.$refs.upload.clearFiles()
    },
    getImage(image) {
      return `http://localhost:9090/hbgc/download?name=${image}`
    },
    reset() {
      this.person = '',
          this.category = "",
          this.dishName = ""
    },
    handleAdd() {
      this.title = "添加"
      this.dialogFormVisible = true
      this.form = {}
    },
    exp() {
      window.open(`http://localhost:9090/hbgc/MydishExport`)
    },
  }
}
</script>

<style scoped>

</style>